from rest_framework.views import APIView,Response
from users.models import UserModel,Chat
from shops.models import Shops



class UserChat(APIView):
    def get(self,request):
        sid = request.query_params.get('sid')
        uid = request.query_params.get('uid')
        chat_id = request.query_params.get('chat_id')
        data = []
        userspk = Chat.objects.filter(shops_id=sid,user_id=uid).first()
        print(">>>>>>>>",userspk)

        speakdata = Chat.objects.filter(room_id=chat_id).first()
        print("ZZZZZZZZZZZZ",speakdata)
        user = UserModel.objects.get(id = speakdata.user.id)

        busin = Shops.objects.get(id = speakdata.shops.id)
        print("MMMMMMMMM",busin)
        datchat = Chat.objects.filter(room_id=chat_id).all()
        for i in datchat:
            dats = {
                'room_id':chat_id,
                'nickname':user.nickname,
                'user_profile_photo':user.profile_photo,
                'user':user.id,
                'shops':busin.id,
                'shops_logo':busin.logo,
                'name':busin.shops_name,
                'content':i.content,
                'status':i.state,
            }
            data.append(dats)
        return Response({'code':200,'msg':'ok','data':data})



# # 根据商家ID去获取房间号里的用户ID
# class ShopChat(APIView):
#     def get(self,request):
#         data = []
#         sid = 1
#         userid = Chat.objects.filter(Q(user=sid)).values("user").distinct()
#         print("=================",userid)
#         for i in userid:
#             data.append(i)
#
#         for j in data:
#             user = UserModel.objects.get(id = j.get("user"))
#
#             j["nickname"] = user.nickname,
#             j["user_id"] = user.id
#         return Response({"code":200,'msg':'获取成功','data':data})